#!/bin/sh
OTA_FILE="/root/gateway.bin"
EXE_FILE="/usr/bin/gateway"
PID_FILE="/var/run/gateway.pid"

start() {
    # OTA检查
    if [ -f $OTA_FILE ]
    then
        chmod +x $OTA_FILE
        mv -f $EXE_FILE "${EXE_FILE}.bak"
        mv -f $OTA_FILE $EXE_FILE
    fi

    # 启动网关进程
	printf "Starting gateway: "
	start-stop-daemon -S -q -p $PID_FILE \
		--exec $EXE_FILE -- daemon
	[ $? = 0 ] && echo "OK" || echo "FAIL"
}
stop() {
	printf "Stopping gateway: "
	start-stop-daemon -K -q -p $PID_FILE
	[ $? = 0 ] && echo "OK" || echo "FAIL"
}

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart|reload)
	stop
	start
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?
